package leetcode.秋招.腾讯;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author ：zsy
 * @date ：Created 2021/9/5 20:48
 * @description：
 */
public class Main2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String next = in.next();
        int count0 = 0, count1 = 0;
        for (int i = 0; i < next.length(); i++) {
            if ('0' == next.charAt(i)) {
                count0++;
            } else
                count1++;
        }
        StringBuilder ans = new StringBuilder();
        if (count0 > count1) {
            StringBuilder builder = new StringBuilder();
            StringBuilder cur = new StringBuilder(next);
            int index = cur.indexOf("0");
            builder.append(cur.substring(0, index));
            cur = new StringBuilder(cur.substring(index));
            cur = cur.reverse();
            int index1 = cur.indexOf("0");
            String substring = cur.substring(0, index1);
            StringBuilder pre = new StringBuilder(cur.substring(index1));
            builder.append(pre.toString().replace('1', ' ')).append(substring);
            String string = builder.toString();
            for (int i = 0; i < builder.length(); i++) {
                if (' ' != string.charAt(i)) {
                    ans.append(string.charAt(i));
                }
            }
        } else {
            StringBuilder builder = new StringBuilder();
            StringBuilder cur = new StringBuilder(next);
            int index = cur.indexOf("1");
            builder.append(cur.substring(0, index));
            cur = new StringBuilder(cur.substring(index));
            cur = cur.reverse();
            int index1 = cur.indexOf("1");
            String substring = cur.substring(0, index1);
            StringBuilder pre = new StringBuilder(cur.substring(index1));
            builder.append(pre.toString().replace('0', ' ')).append(substring);
            String string = builder.toString();
            for (int i = 0; i < builder.length(); i++) {
                if (' ' != string.charAt(i)) {
                    ans.append(string.charAt(i));
                }
            }
        }
        System.out.println(val(ans));
    }

    private static int val(StringBuilder ans) {
        int n = ans.length();
        int[] vals = new int[n];
        vals[0] = 1;
        char[] chars = ans.toString().toCharArray();
        char pre = chars[0];
        for (int i = 1; i < n; i++) {
            if (chars[i] == pre) {
                vals[i] = vals[i - 1] + 1;
            } else {
                vals[i] = 1;
                pre = chars[i];
            }
        }
        return Arrays.stream(vals).sum();
    }
}
